home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / bash-1.08 / general.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-22  |  18.4 KB  |  863 lines

  1. /* general.c -- Stuff that is used by all files. */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <sys/types.h>
  24. #include <sys/param.h>
  25. #include <fcntl.h>
  26.  
  27. #include "shell.h"
  28. #if defined (USG)
  29. #include <string.h>
  30. #else
  31. #include <strings.h>
  32. #endif /* USG */
  33.  
  34. #if !defined (USG) || defined (HAVE_RESOURCE)
  35. #include <sys/time.h>
  36. #endif
  37.  
  38. #include <sys/times.h>
  39.  
  40. #ifndef NULL
  41. #define NULL 0x0
  42. #endif
  43.  
  44. extern int errno;
  45.  
  46. /* **************************************************************** */
  47. /*                                    */
  48. /*           Memory Allocation and Deallocation.            */
  49. /*                                    */
  50. /* **************************************************************** */
  51.  
  52. char *
  53. xmalloc (size)
  54.      int size;
  55. {
  56.   register char *temp = (char *)malloc (size);
  57.  
  58.   if (!temp)
  59.     fatal_error ("Out of virtual memory!");
  60.  
  61.   return (temp);
  62. }
  63.  
  64. char *
  65. xrealloc (pointer, size)
  66.      register char *pointer;
  67.      int size;
  68. {
  69.   char *temp;
  70.  
  71.   if (!pointer)
  72.     temp = (char *)xmalloc (size);
  73.   else
  74.     temp = (char *)realloc (pointer, size);
  75.  
  76.   if (!temp)
  77.     fatal_error ("Out of virtual memory!");
  78.  
  79.   return (temp);
  80. }
  81.  
  82.  
  83. /* **************************************************************** */
  84. /*                                    */
  85. /*             Integer to String Conversion            */
  86. /*                                    */
  87. /* **************************************************************** */
  88.  
  89. /* Number of characters that can appear in a string representation
  90.    of an integer.  32 is larger than the string rep of 2^^31 - 1. */
  91. #define MAX_INT_LEN 32
  92.  
  93. /* Integer to string conversion.  This conses the string; the
  94.    caller should free it. */
  95. char *
  96. itos (i)
  97.      int i;
  98. {
  99.   char *buf, *p, *ret;
  100.   int negative = 0;
  101.   unsigned int ui;
  102.  
  103.   buf = xmalloc (MAX_INT_LEN);
  104.  
  105.   if (i < 0)
  106.     {
  107.       negative++;
  108.       i = -i;
  109.     }
  110.  
  111.   ui = (unsigned int) i;
  112.  
  113.   buf[MAX_INT_LEN - 1] = '\0';
  114.   p = &buf[MAX_INT_LEN - 2];
  115.  
  116.   do
  117.     *p-- = (ui % 10) + '0';
  118.   while (ui /= 10);
  119.  
  120.   if (negative)
  121.     *p-- = '-';
  122.  
  123.   ret = savestring (p + 1);
  124.   free (buf);
  125.   return (ret);
  126. }
  127.  
  128. /* A function to unset no-delay mode on a file descriptor.  Used in shell.c
  129.    to unset it on the fd passed as stdin.  Should be called on stdin if
  130.    readline gets an EAGAIN or EWOULDBLOCK when trying to read input. */
  131.  
  132. #if !defined (O_NDELAY)
  133. #  if defined (FNDELAY)
  134. #    define O_NDELAY FNDELAY
  135. #  endif
  136. #endif /* O_NDELAY */
  137.  
  138. /* Make sure no-delay mode is not set on file descriptor FD. */
  139. void
  140. unset_nodelay_mode (fd)
  141.      int fd;
  142. {
  143.   int flags, set = 0;
  144.  
  145.   if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
  146.     return;
  147.  
  148. #if defined (O_NONBLOCK)
  149.   if (flags & O_NONBLOCK)
  150.     {
  151.       flags &= ~O_NONBLOCK;
  152.       set++;
  153.     }
  154. #endif /* O_NONBLOCK */
  155.  
  156. #if defined (O_NDELAY)
  157.   if (flags & O_NDELAY)
  158.     {
  159.       flags &= ~O_NDELAY;
  160.       set++;
  161.     }
  162. #endif /* O_NDELAY */
  163.  
  164.   if (set)
  165.     fcntl (fd, F_SETFL, flags);
  166. }
  167.  
  168.  
  169. /* **************************************************************** */
  170. /*                                    */
  171. /*            Generic List Functions                */
  172. /*                                    */
  173. /* **************************************************************** */
  174.  
  175. /* Call FUNCTION on every member of LIST, a generic list. */
  176. map_over_list (list, function)
  177.      GENERIC_LIST *list;
  178.      Function *function;
  179. {
  180.   while (list) {
  181.     (*function) (list);
  182.     list = list->next;
  183.   }
  184. }
  185.  
  186. /* Call FUNCTION on every string in WORDS. */
  187. map_over_words (words, function)
  188.      WORD_LIST *words;
  189.      Function *function;
  190. {
  191.   while (words) {
  192.     (*function)(words->word->word);
  193.     words = words->next;
  194.   }
  195. }
  196.  
  197. /* Reverse the chain of structures in LIST.  Output the new head
  198.    of the chain.  You should always assign the output value of this
  199.    function to something, or you will lose the chain. */
  200. GENERIC_LIST *
  201. reverse_list (list)
  202.      register GENERIC_LIST *list;
  203. {
  204.   register GENERIC_LIST *next, *prev = (GENERIC_LIST *)NULL;
  205.  
  206.   while (list) {
  207.     next = list->next;
  208.     list->next = prev;
  209.     prev = list;
  210.     list = next;
  211.   }
  212.   return (prev);
  213. }
  214.  
  215. /* Return the number of elements in LIST, a generic list. */
  216. list_length (list)
  217.      register GENERIC_LIST *list;
  218. {
  219.   register int i;
  220.  
  221.   for (i = 0; list; list = list->next, i++);
  222.   return (i);
  223. }
  224.  
  225. /* Delete the element of LIST which satisfies the predicate function COMPARER.
  226.    Returns the element that was deleted, so you can dispose of it, or -1 if
  227.    the element wasn't found.  COMPARER is called with the list element and
  228.    then ARG.  Note that LIST contains the address of a variable which points
  229.    to the list.  You might call this function like this:
  230.  
  231.    SHELL_VAR *elt = delete_element (&variable_list, check_var_has_name, "foo");
  232.    dispose_variable (elt);
  233. */
  234. GENERIC_LIST *
  235. delete_element (list, comparer, arg)
  236.      GENERIC_LIST **list;
  237.      Function *comparer;
  238. {
  239.   register GENERIC_LIST *prev = (GENERIC_LIST *)NULL;
  240.   register GENERIC_LIST *temp = *list;
  241.  
  242.   while (temp) {
  243.     if ((*comparer) (temp, arg)) {
  244.       if (prev) prev->next = temp->next;
  245.       else *list = temp->next;
  246.       return (temp);
  247.     }
  248.     prev = temp;
  249.     temp = temp->next;
  250.   }
  251.   return ((GENERIC_LIST *)-1);
  252. }
  253.  
  254. /* Find NAME in ARRAY.  Return the index of NAME, or -1 if not present.
  255.    ARRAY shoudl be NULL terminated. */
  256. find_name_in_list (name, array)
  257.      char *name, *array[];
  258. {
  259.   int i;
  260.  
  261.   for (i=0; array[i]; i++)
  262.     if (strcmp (name, array[i]) == 0)
  263.       return (i);
  264.  
  265.   return (-1);
  266. }
  267.  
  268. /* Return the length of ARRAY, a NULL terminated array of char *. */
  269. array_len (array)
  270.      register char **array;
  271. {
  272.   register int i;
  273.   for (i=0; array[i]; i++);
  274.   return (i);
  275. }
  276.  
  277. /* Free the contents of ARRAY, a NULL terminated array of char *. */
  278. free_array (array)
  279.      register char **array;
  280. {
  281.   register int i = 0;
  282.  
  283.   if (!array) return;
  284.  
  285.   while (array[i])
  286.     free (array[i++]);
  287.   free (array);
  288. }
  289.  
  290. /* Append LIST2 to LIST1.  Return the header of the list. */
  291. GENERIC_LIST *
  292. list_append (head, tail)
  293.      GENERIC_LIST *head, *tail;
  294. {
  295.   register GENERIC_LIST *t_head = head;
  296.  
  297.   if (!t_head)
  298.     return (tail);
  299.  
  300.   while (t_head->next) t_head = t_head->next;
  301.   t_head->next = tail;
  302.   return (head);
  303. }
  304.  
  305. /* Some random string stuff. */
  306.  
  307. /* Remove all leading whitespace from STRING.  This includes
  308.    newlines.  STRING should be terminated with a zero. */
  309. strip_leading (string)
  310.      char *string;
  311. {
  312.   char *start = string;
  313.  
  314.   while (*string && (whitespace (*string) || *string == '\n')) string++;
  315.  
  316.   if (string != start)
  317.     {
  318.       int len = strlen (string);
  319.       bcopy (string, start, len);
  320.       start[len] = '\0';
  321.     }
  322. }
  323.  
  324. /* Remove all trailing whitespace from STRING.  This includes
  325.    newlines.  If NEWLINES_ONLY is non-zero, only trailing newlines
  326.    are removed.  STRING should be terminated with a zero. */
  327. strip_trailing (string, newlines_only)
  328.      char *string;
  329.      int newlines_only;
  330. {
  331.   int len = strlen (string) - 1;
  332.  
  333.   while (len >= 0)
  334.     {
  335.       if ((newlines_only && string[len] == '\n') ||
  336.           (!newlines_only && whitespace (string[len])))
  337.         len--;
  338.       else
  339.         break;
  340.     }
  341.   string[len + 1] = '\0';
  342. }
  343.  
  344. /* Turn STRING (a pathname) into an absolute pathname, assuming that
  345.    DOT_PATH contains the symbolic location of '.'.  This always
  346.    returns a new string, even if STRING was an absolute pathname to
  347.    begin with. */
  348.  
  349. #ifndef MAXPATHLEN
  350. #define MAXPATHLEN 1024
  351. #endif
  352.  
  353. static char current_path[MAXPATHLEN];
  354.  
  355. char *
  356. make_absolute (string, dot_path)
  357.      char *string, *dot_path;
  358. {
  359.   register char *cp;
  360.  
  361.   if (!dot_path || *string == '/')
  362.     return (savestring (string));
  363.  
  364.   strcpy (current_path, dot_path);
  365.  
  366.   if (!current_path[0])
  367.     strcpy (current_path, "./");
  368.  
  369.   cp = current_path + (strlen (current_path) - 1);
  370.  
  371.   if (*cp++ != '/')
  372.     *cp++ = '/';
  373.  
  374.   *cp = '\0';
  375.  
  376.   while (*string)
  377.     {
  378.       if (*string == '.')
  379.     {
  380.       if (!string[1])
  381.         return (savestring (current_path));
  382.  
  383.       if (string[1] == '/')
  384.         {
  385.           string += 2;
  386.           continue;
  387.         }
  388.  
  389.       if (string[1] == '.' && (string[2] == '/' || !string[2]))
  390.         {
  391.           string += 2;
  392.  
  393.           if (*string)
  394.         string++;
  395.  
  396.           pathname_backup (current_path, 1);
  397.           cp = current_path + strlen (current_path);
  398.           continue;
  399.         }
  400.     }
  401.  
  402.       while (*string && *string != '/')
  403.     *cp++ = *string++;
  404.  
  405.       if (*string)
  406.     *cp++ = *string++;
  407.  
  408.       *cp = '\0';
  409.     }
  410.   return (savestring (current_path));
  411. }
  412.  
  413. /* Remove the last N directories from PATH.  Do not PATH blank.
  414.    PATH must contain enoung space for MAXPATHLEN characters. */
  415. pathname_backup (path, n)
  416.      char *path;
  417.      int n;
  418. {
  419.   register char *p;
  420.  
  421.   if (!*path)
  422.     return;
  423.  
  424.   p = path + (strlen (path) - 1);
  425.  
  426.   while (n--)
  427.     {
  428.       while (*p == '/' && p != path)
  429.     p--;
  430.  
  431.       while (*p != '/' && p != path)
  432.     p--;
  433.  
  434.       *++p = '\0';
  435.     }
  436. }
  437.  
  438. /* Return 1 if STRING contains an absolute pathname, else 0. */
  439. absolute_pathname (string)
  440.      char *string;
  441. {
  442.   if (!string || !*string)
  443.     return (0);
  444.  
  445.   if (*string == '/')
  446.     return (1);
  447.  
  448.   if (*string++ == '.')
  449.     {
  450.       if ((!*string) || *string == '/')
  451.     return (1);
  452.  
  453.       if (*string++ == '.')
  454.     if (!*string || *string == '/')
  455.       return (1);
  456.     }
  457.   return (0);
  458. }
  459.  
  460. /* Return 1 if STRING is an absolute program name; it is absolute if it
  461.    contains any slashes.  This is used to decide whether or not to look
  462.    up through $PATH. */
  463. absolute_program (string)
  464.      char *string;
  465. {
  466.   return (index (string, '/') != NULL);
  467. }
  468.  
  469. /* Return the `basename' of the pathname in STRING (the stuff after the
  470.    last '/').  If STRING is not a full pathname, simply return it. */
  471. char *
  472. base_pathname (string)
  473.      char *string;
  474. {
  475.   char *p = rindex (string, '/');
  476.  
  477.   if (!absolute_pathname(string))
  478.     return (string);
  479.  
  480.   if (p)
  481.     return (++p);
  482.   else
  483.     return (string);
  484. }
  485.  
  486. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  487.    match in s1.  The compare is case insensitive. */
  488. char *
  489. strindex (s1, s2)
  490.      register char *s1, *s2;
  491. {
  492.   register int i, l = strlen (s2);
  493.   register int len = strlen (s1);
  494.  
  495.   for (i = 0; (len - i) >= l; i++)
  496.     if (strnicmp (&s1[i], s2, l) == 0)
  497.       return (s1 + i);
  498.   return ((char *)NULL);
  499. }
  500.  
  501. #if !defined (to_upper)
  502. #define lowercase_p(c) (((c) > ('a' - 1) && (c) < ('z' + 1)))
  503. #define uppercase_p(c) (((c) > ('A' - 1) && (c) < ('Z' + 1)))
  504. #define pure_alphabetic(c) (lowercase_p(c) || uppercase_p(c))
  505. #define to_upper(c) (lowercase_p(c) ? ((c) - 32) : (c))
  506. #define to_lower(c) (uppercase_p(c) ? ((c) + 32) : (c))
  507. #endif /* to_upper */
  508.  
  509. /* Compare at most COUNT characters from string1 to string2.  Case
  510.    doesn't matter. */
  511. int
  512. strnicmp (string1, string2, count)
  513.      char *string1, *string2;
  514. {
  515.   register char ch1, ch2;
  516.  
  517.   while (count) {
  518.     ch1 = *string1++;
  519.     ch2 = *string2++;
  520.     if (to_upper(ch1) == to_upper(ch2))
  521.       count--;
  522.     else break;
  523.   }
  524.   return (count);
  525. }
  526.  
  527. /* strcmp (), but caseless. */
  528. int
  529. stricmp (string1, string2)
  530.      char *string1, *string2;
  531. {
  532.   register char ch1, ch2;
  533.  
  534.   while (*string1 && *string2) {
  535.     ch1 = *string1++;
  536.     ch2 = *string2++;
  537.     if (to_upper(ch1) != to_upper(ch2))
  538.       return (1);
  539.   }
  540.   return (*string1 | *string2);
  541. }
  542.  
  543. /* Return a string corresponding to the error number E.  From
  544.    the ANSI C spec. */
  545. #if defined (strerror)
  546. #undef strerror
  547. #endif
  548.  
  549. #if !defined (HAVE_STRERROR)
  550. char *
  551. strerror (e)
  552.      int e;
  553. {
  554.   extern int sys_nerr;
  555.   extern char *sys_errlist[];
  556.   static char emsg[40];
  557.  
  558.   if (e > 0 && e < sys_nerr)
  559.     return (sys_errlist[e]);
  560.   else
  561.     {
  562.       sprintf (emsg, "Unknown error %d", e);
  563.       return (&emsg[0]);
  564.     }
  565. }
  566. #endif /* HAVE_STRERROR */
  567.  
  568. #if !defined (USG) || defined (HAVE_RESOURCE)
  569. /* Print the contents of a struct timeval * in a standard way. */
  570. print_timeval (tvp)
  571.      struct timeval *tvp;
  572. {
  573.   int minutes, seconds_fraction;
  574.   long seconds;
  575.  
  576.   seconds = tvp->tv_sec;
  577.  
  578.   seconds_fraction = tvp->tv_usec % 1000000;
  579.   seconds_fraction = (seconds_fraction * 100) / 1000000;
  580.  
  581.   minutes = seconds / 60;
  582.   seconds %= 60;
  583.  
  584.   printf ("%0dm%0d.%02ds",  minutes, seconds, seconds_fraction);
  585. }
  586. #endif
  587.  
  588. /* Print the time defined by a time_t (returned by the `times' and `time'
  589.    system calls) in a standard way.  This is scaled in terms of HZ, which
  590.    is what is returned by the `times' call. */
  591.  
  592. #if !defined (BrainDeath)
  593. #if !defined (HZ)
  594. #  if defined (USG)
  595. #    define HZ 100        /* From my Sys V.3.2 manual for times(2) */
  596. #  else
  597. #    define HZ 60        /* HZ is always 60 on BSD systems */
  598. #  endif /* USG */
  599. #endif /* HZ */
  600.  
  601. print_time_in_hz (t)
  602.   time_t t;
  603. {
  604.   int minutes, seconds_fraction;
  605.   long seconds;
  606.  
  607.   seconds_fraction = t % HZ;
  608.   seconds_fraction = (seconds_fraction * 100) / HZ;
  609.  
  610.   seconds = t / HZ;
  611.  
  612.   minutes = seconds / 60;
  613.   seconds %= 60;
  614.  
  615.   printf ("%0dm%0d.%02ds",  minutes, seconds, seconds_fraction);
  616. }
  617. #endif /* BrainDeath */
  618.  
  619. #if !defined (HAVE_DUP2)
  620. /* Replacement for dup2 (), for those systems which either don't have it,
  621.    or supply one with broken behaviour. */
  622. int
  623. dup2 (fd1, fd2)
  624.      int fd1, fd2;
  625. {
  626.   int saved_errno, r;
  627.  
  628.   /* If FD1 is not a valid file descriptor, then return immediately with
  629.      an error. */
  630.   if (fcntl (fd1, F_GETFL, 0) == -1)
  631.     return (-1);
  632.  
  633.   if (fd2 < 0 || fd2 >= getdtablesize ())
  634.     {
  635.       errno = EBADF;
  636.       return (-1);
  637.     }
  638.  
  639.   if (fd1 == fd2)
  640.     return (0);
  641.  
  642.   saved_errno = errno;
  643.  
  644.   (void) close (fd2);
  645.   r = fcntl (fd1, F_DUPFD, fd2);
  646.  
  647.   if (r >= 0)
  648.     errno = saved_errno;
  649.   else
  650.     if (errno == EINVAL)
  651.       errno = EBADF;
  652.  
  653.   /* Force the new file descriptor to remain open across exec () calls. */
  654.   SET_OPEN_ON_EXEC (fd2);
  655.   return (r);
  656. }
  657. #endif /* !HAVE_DUP2 */
  658.  
  659. /*
  660.  * Return the total number of available file descriptors.
  661.  *
  662.  * On some systems, like 4.2BSD and its descendents, there is a system call
  663.  * that returns the size of the descriptor table: getdtablesize().  There are
  664.  * lots of ways to emulate this on non-BSD systems.
  665.  *
  666.  * On System V.3, this can be obtained via a call to ulimit:
  667.  *    return (ulimit(4, 0L));
  668.  *
  669.  * On other System V systems, NOFILE is defined in /usr/include/sys/param.h
  670.  * (this is what we assume below), so we can simply use it:
  671.  *    return (NOFILE);
  672.  *
  673.  * On POSIX systems, there are specific functions for retrieving various
  674.  * configuration parameters:
  675.  *    return (sysconf(_SC_OPEN_MAX));
  676.  *
  677.  */
  678.  
  679. #if defined (USG) || defined (HPUX)
  680. int
  681. getdtablesize ()
  682. {
  683. #if defined (_POSIX_VERSION)
  684.   return (sysconf(_SC_OPEN_MAX));    /* Posix systems use sysconf */
  685. #else
  686. #  if defined (USGr3)
  687.   return (ulimit (4, 0L));    /* System V.3 systems use ulimit(4, 0L) */
  688. #  else
  689. #    if defined (NOFILE)    /* Other systems use NOFILE */
  690.   return (NOFILE);
  691. #    else
  692.   return (20);            /* XXX - traditional value is 20 */
  693. #    endif /* NOFILE */
  694. #  endif /* USGr3 */
  695. #endif /* _POSIX_VERSION */
  696. }
  697. #endif /* USG && !defined USGr4 */
  698.  
  699. #if defined (USG) && !defined (sgi)
  700.  
  701. #if !defined (RISC6000)
  702. bcopy(s,d,n) char *d,*s; { memcpy (d, s, n); }
  703. bzero(s,n) char *s; int n; { memset(s, '\0', n); }
  704. #endif /* RISC6000 */
  705.  
  706. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  707. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  708.  
  709. #if !defined (HAVE_GETWD)
  710. char *
  711. getwd (string)
  712.      char *string;
  713. {
  714.   extern char *getcwd ();
  715.   char *result;
  716.  
  717.   result = getcwd (string, MAXPATHLEN);
  718.   if (result == NULL)
  719.     strcpy (string, "getwd: cannot access parent directories");
  720.   return (result);
  721. }
  722. #endif /* !HAVE_GETWD */
  723.  
  724. #if !defined (HPUX)
  725. #include <sys/utsname.h>
  726. gethostname (name, namelen)
  727.      char *name;
  728.      int namelen;
  729. {
  730.   int i;
  731.   struct utsname uts;
  732.  
  733.   --namelen;
  734.  
  735.   uname (&uts);
  736.   i = strlen (uts.nodename) + 1;
  737.   strncpy (name, uts.nodename, i < namelen ? i : namelen);
  738.   name[namelen] = '\0';
  739.   return (0);
  740. }
  741. #endif /* !HPUX */
  742. #endif /* USG && !sgi */
  743.  
  744. #if defined (USG) || defined (_POSIX_VERSION)
  745. int
  746. sysv_getc (stream)
  747.      FILE *stream;
  748. {
  749.   int result;
  750.   char c;
  751.  
  752.   while (1)
  753.     {
  754.       result = read (fileno (stream), &c, sizeof (char));
  755.  
  756.       if (result == 0)
  757.     return (EOF);
  758.  
  759.       if (result == sizeof (char))
  760.     return (c);
  761.  
  762.       if (errno != EINTR)
  763.     return (EOF);
  764.     }
  765. }
  766.  
  767. /* USG and POSIX systems do not have killpg ().  But we use it in
  768.    jobs.c, nojobs.c and builtins.c. */
  769. #if !defined (_POSIX_VERSION)
  770. #define pid_t int
  771. #endif /* _POSIX_VERSION */
  772.  
  773. int
  774. killpg (pgrp, sig)
  775.      pid_t pgrp;
  776.      int sig;
  777. {
  778.   int result;
  779.  
  780.   result = kill (-pgrp, sig);
  781.   return (result);
  782. }
  783. #endif /* USG  || _POSIX_VERSION */
  784.  
  785. #if !defined (READLINE)
  786. /* Expand FILENAME if it begins with a tilde.  This always returns
  787.    a new string. */
  788. char *
  789. tilde_expand (filename)
  790.      char *filename;
  791. {
  792.   char *dirname = filename ? savestring (filename) : (char *)NULL;
  793.  
  794.   if (dirname && *dirname == '~')
  795.     {
  796.       char *temp_name;
  797.       if (!dirname[1] || dirname[1] == '/')
  798.     {
  799.       /* Prepend $HOME to the rest of the string. */
  800.       char *temp_home = (char *)getenv ("HOME");
  801.  
  802.       temp_name = (char *)alloca (1 + strlen (&dirname[1])
  803.                       + (temp_home? strlen (temp_home) : 0));
  804.       temp_name[0] = '\0';
  805.       if (temp_home)
  806.         strcpy (temp_name, temp_home);
  807.       strcat (temp_name, &dirname[1]);
  808.       free (dirname);
  809.       dirname = savestring (temp_name);
  810.     }
  811.       else
  812.     {
  813.       struct passwd *getpwnam (), *user_entry;
  814.       char *username = (char *)alloca (257);
  815.       int i, c;
  816.  
  817.       for (i = 1; c = dirname[i]; i++)
  818.         {
  819.           if (c == '/') break;
  820.           else username[i - 1] = c;
  821.         }
  822.       username[i - 1] = '\0';
  823.  
  824.       if (!(user_entry = getpwnam (username)))
  825.         {
  826.           /* If the calling program has a special syntax for
  827.          expanding tildes, and we couldn't find a standard
  828.          expansion, then let them try. */
  829.           if (rl_tilde_expander)
  830.         {
  831.           char *expansion;
  832.  
  833.           expansion = (char *)(*rl_tilde_expander) (username);
  834.  
  835.           if (expansion)
  836.             {
  837.               temp_name = (char *)alloca (1 + strlen (expansion)
  838.                           + strlen (&dirname[i]));
  839.               strcpy (temp_name, expansion);
  840.               strcat (temp_name, &dirname[i]);
  841.               free (expansion);
  842.               goto return_name;
  843.             }
  844.         }
  845.           /* We shouldn't report errors. */
  846.         }
  847.       else
  848.         {
  849.           temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir)
  850.                       + strlen (&dirname[i]));
  851.           strcpy (temp_name, user_entry->pw_dir);
  852.           strcat (temp_name, &dirname[i]);
  853.         return_name:
  854.           free (dirname);
  855.           dirname = savestring (temp_name);
  856.         }
  857.       endpwent ();
  858.     }
  859.     }
  860.   return (dirname);
  861. }
  862. #endif /* !READLINE */
  863.